(function() {

    var stringModule = {

        capitalize : function(value) {
            return value.charAt(0).toUpperCase() + value.substring(1);
        },

        trim : function(value) {
            var
            result = value.replace(/^\s\s*/, ''),
            space = /\s/,
            i = result.length;

            while (space.test(result.charAt(--i))) { }

            return result.slice(0, i + 1);
        },

        isEmpty : function(value) {
            return ((value === null) || (value === ''));
        },

        insertBreaks : function(value) {
            return value.replace(/(\r\n|\n\r|\n|\r)/g, '<br />');
        },

        format : function(text, data, ifNull) {
            return text.replace(/\${(.*?)}/gim, function(value, p1) {
                var
                matchParts = p1.split(':'),
                operation = matchParts[1],
                variableParts = matchParts[0].split('.'),
                result = data[variableParts[0]],
                i;

                for (i = 1; i < variableParts.length; ++i) {
                    if (result) result = result[variableParts[i]];
                }

                if (ifNull && (result === null)) {
                    return ifNull;
                }

                if (result) {
                    if (operation === 'insert-breaks') {
                        result = stringModule.insertBreaks(result);
                    }
                }

                return result;
            });
        }

    };

    juliet.registerModule('juliet.core', 'string-0.1', stringModule);

})();
